package com.chengxiaoxiao.service.impl;

import com.chengxiaoxiao.commons.Page;
import com.chengxiaoxiao.dao.BorrowDao;
import com.chengxiaoxiao.dao.impl.BorrowDaoImpl;
import com.chengxiaoxiao.domain.Books;
import com.chengxiaoxiao.domain.Borrow;
import com.chengxiaoxiao.domain.json.Author;
import com.chengxiaoxiao.domain.json.AuthorAndImage;
import com.chengxiaoxiao.service.BorrowService;

import java.util.List;

public class BorrowServiceImpl implements BorrowService {
    private BorrowDao dao = new BorrowDaoImpl();

    @Override
    public void add(Borrow borrow) {
        dao.add(borrow);
    }

    @Override
    public void update(Borrow borrow) {
        dao.update(borrow);
    }

    @Override
    public void delete(long id) {
        dao.delete(id);
    }

    @Override
    public List get() {
        return dao.get();
    }

    @Override
    public Borrow getbyId(long id) {
        return dao.getbyId(id);
    }

    @Override
    public Page getListPage(String num) {

        int pageNum = 1;
        if (null != num && num != "") {
            pageNum = Integer.parseInt(num);
        }

        int total = dao.getTotalRecordsNum();
        Page page = new Page(pageNum, total);
        List<Borrow> list = dao.getListPage(page.getStartIndex(), page.getPageSize());

        page.setRecords(list);

        return page;
    }

    @Override
    public Page getListPageBorrow(String num) {
        int pageNum = 1;
        if (null != num && num != "") {
            pageNum = Integer.parseInt(num);
        }

        int total = dao.getTotalRecordsNumBorrow();
        Page page = new Page(pageNum, total);
        List<Borrow> list = dao.getListPageBorrow(page.getStartIndex(), page.getPageSize());

        page.setRecords(list);

        return page;
    }

    @Override
    public Page getListPageByNoReturn(String num) {

        int pageNum = 1;
        if (null != num && num != "") {
            pageNum = Integer.parseInt(num);
        }

        int total = dao.getTotalRecordsNumNoReturn();
        Page page = new Page(pageNum, total);
        List<Borrow> list = dao.getListPageBorrowNoReturn(page.getStartIndex(), page.getPageSize());

        page.setRecords(list);

        return page;
    }

    @Override
    public Page getListPageBorrowRank(String num) {
        int pageNum = 1;
        if (null != num && num != "") {
            pageNum = Integer.parseInt(num);
        }

        int total = dao.getTotalRecordsNumRank();
        Page page = new Page(pageNum, total);
        List<Books> list = dao.getListPageBorrowRank(page.getStartIndex(), page.getPageSize());

        page.setRecords(list);

        return page;

    }

    @Override
    public Page getListPageDestory(String num) {

        int pageNum = 1;
        if (null != num && num != "") {
            pageNum = Integer.parseInt(num);
        }

        int total = dao.getTotalRecordsNumDestory();
        Page page = new Page(pageNum, total);
        List<Borrow> list = dao.getListPageBorrowDestory(page.getStartIndex(), page.getPageSize());

        page.setRecords(list);

        return page;
    }

    @Override
    public Page getListPageNewRank(String num) {

        int pageNum = 1;
        if (null != num && num != "") {
            pageNum = Integer.parseInt(num);
        }

        int total = dao.getTotalRecordsNum();
        Page page = new Page(pageNum, total);
        List<Books> list = dao.getListPageNewBook(page.getStartIndex(), page.getPageSize());

        page.setRecords(list);

        return page;
    }

    @Override
    public Page getListPageHotAuthor(String num) {
        int pageNum = 1;
        if (null != num && num != "") {
            pageNum = Integer.parseInt(num);
        }

        int total = dao.getTotalHotAuthor();
        Page page = new Page(pageNum, total);
        List<Author> list = dao.getListPageHotAuthor(page.getStartIndex(), page.getPageSize());

        page.setRecords(list);

        return page;
    }

    @Override
    public AuthorAndImage getAuthorAndImage(String author) {
        return dao.getAuthorAndImage(author);

    }

    @Override
    public int getBorrowCountByUser(int uid) {
        return dao.getBorrowCountByUser(uid);
    }

    @Override
    public Borrow getBorrow(String userid, String bookId) {
        return dao.getBorrow(userid,bookId);
    }

    @Override
    public List<Borrow> getBorrow(String userId) {
        return dao.getBorrow(userId);
    }

    @Override
    public Borrow getBorrowNoReturn(String s, String bookId) {
        return dao.getBorrowNoReturn(s,bookId);
    }

    @Override
    public List<Borrow> getExpire() {
        return dao.getExpire();
    }
}
